package com.vote.business.service.impl;

import java.util.List;
import java.util.Objects;

import com.vote.business.domain.VoteInfo;
import com.vote.business.domain.vo.VoteOptionWithTitleVO;
import com.vote.business.mapper.VoteInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.vote.business.mapper.VoteOptionMapper;
import com.vote.business.domain.VoteOption;
import com.vote.business.service.IVoteOptionService;

/**
 * 投票选项Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-14
 */
@Service
public class VoteOptionServiceImpl implements IVoteOptionService 
{
    @Autowired
    private VoteOptionMapper voteOptionMapper;

    @Autowired
    private VoteInfoMapper voteInfoMapper;
    /**
     * 查询投票选项
     * 
     * @param optionId 投票选项主键
     * @return 投票选项
     */
    @Override
    public VoteOptionWithTitleVO selectVoteOptionByOptionId(Long optionId)
    {
        return voteOptionMapper.selectVoteOptionByOptionId(optionId);
    }

    /**
     * 查询投票选项列表
     * 
     * @param voteOption 投票选项
     * @return 投票选项
     */
    @Override
    public List<VoteOption> selectVoteOptionList(VoteOption voteOption)
    {
        return voteOptionMapper.selectVoteOptionList(voteOption);
    }

    @Override
    public List<VoteOptionWithTitleVO> selectVoteOptionWithTitleDTOList(VoteOptionWithTitleVO voteOptionWithTitleDTO) {
        return voteOptionMapper.selectVoteOptionWithTitleDTOList(voteOptionWithTitleDTO);
    }

    /**
     * 新增投票选项
     * 
     * @param voteOption 投票选项
     * @return 结果
     */
    @Override
    public int insertVoteOption(VoteOption voteOption)
    {
        return voteOptionMapper.insertVoteOption(voteOption);
    }

    /**
     * 修改投票选项
     * 
     * @param voteOption 投票选项
     * @return 结果
     */
    @Override
    public int updateVoteOption(VoteOption voteOption)
    {
        return voteOptionMapper.updateVoteOption(voteOption);
    }

    /**
     * 批量删除投票选项
     * 
     * @param optionIds 需要删除的投票选项主键
     * @return 结果
     */
    @Override
    public int deleteVoteOptionByOptionIds(Long[] optionIds)
    {
        return voteOptionMapper.deleteVoteOptionByOptionIds(optionIds);
    }

    /**
     * 删除投票选项信息
     * 
     * @param optionId 投票选项主键
     * @return 结果
     */
    @Override
    public int deleteVoteOptionByOptionId(Long optionId)
    {
        return voteOptionMapper.deleteVoteOptionByOptionId(optionId);
    }
    /**
     * 检查投票状态是否允许修改选项
     * 只有草稿(0)和发布(1)状态可以修改
     */
    @Override
    public boolean checkVoteStatusForModify(Long voteId) {
        VoteInfo voteInfo = voteInfoMapper.selectVoteInfoByVoteId(voteId);
        if (voteInfo == null) {
            return false;
        }
        // 状态为0(草稿)或1(发布)可以修改
        return voteInfo.getStatus()==0||voteInfo.getStatus()==1;
    }

    /**
     * 检查投票状态是否允许删除选项
     * 只有草稿(0)状态可以删除
     */
    @Override
    public boolean checkVoteStatusForDelete(Long[] optionIds) {
        // 获取选项对应的投票ID
        VoteOptionWithTitleVO voteOptionWithTitleDTO = voteOptionMapper.selectVoteOptionByOptionId(optionIds[0]);
        VoteInfo voteInfo = voteInfoMapper.selectVoteInfoByVoteId(voteOptionWithTitleDTO.getVoteId());
        if (voteInfo == null) {
            return false;
        }
        // 只有状态为0(草稿)可以删除
        return Objects.equals(voteInfo.getStatus(), "0");
    }

    @Override
    public int incrementVoteCount(Long optionId) {
        return voteOptionMapper.incrementVoteCount(optionId);
    }
    /**
     * 更新投票记录，更新投票选项票数
     * @param oldOptionId
     * @param optionId
     */
    @Override
    public int updateVoteOptionCount(Long oldOptionId) {
        return voteOptionMapper.updateVoteOptionCount(oldOptionId);
    }

}
